package ext.tianma.doc.forms;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Map;

import com.infoengine.object.IeAtt;
import com.infoengine.object.IeDatum;
import com.infoengine.object.IeObject;

/**
 * 通过给定map的Key和Value值来获取相应中间表的值
 * @author KWang
 *
 */
public class RFQCreateUtil {

	public static Hashtable<String,String> updateRFQDocIBA( IeObject ieoRecord ){		
		Hashtable<String,String> table = getIBAMapping( RFQXMLUtil.getInstance().getRfqMapping() ,  ieoRecord) ;
		
		if(table == null){
			return new Hashtable<String,String>() ;
		}
		
		return table ;
	}
	
	public static Hashtable<String,String> updateRFQDocRelatedFinishedGoodIBA( IeObject ieoRecord ){
		Hashtable<String,String> table = getIBAMapping( RFQXMLUtil.getInstance().getFinishedGoodMapping() ,  ieoRecord) ;
		
		if(table == null){
			return new Hashtable<String,String>() ;
		}
		
		return table ;
	}
	
	private static Hashtable<String,String> getIBAMapping(Map<String,String> map ,  IeObject ieoRecord){
		Hashtable<String,String> table = new Hashtable<String,String>() ;
		
		if( map == null ){
			return table ;
		}
		
		Iterator<String> ite = map.keySet().iterator() ;
		
		while( ite.hasNext() ){
			String dbColumn = ite.next() ;
			
			String ibaName = map.get(dbColumn);
			
			String dbColumnValue = getIeObjectValue( ieoRecord , dbColumn ) ;
			
			addToHashtable( table , ibaName , dbColumnValue ) ;
		}
		
		return table ;
	}
	
	private static String getIeObjectValue( IeObject ieoRecord , String dbColumn ){
		String dbColumnValue = "" ; 
		
		if( ieoRecord == null ){
			return dbColumnValue ;
		}
		
		if(dbColumn == null || dbColumn.trim().equals("")){
			return dbColumnValue ;
		}
		
		IeAtt ieAtt = ieoRecord.getAtt( dbColumn ) ;
		
		if( ieAtt != null ){
			IeDatum ieDatum = ieAtt.getDatum();
			
			if( ieDatum != null ){
				dbColumnValue = ieDatum.toString() ;
			}
		}
		
		return dbColumnValue ;
	}
	
	private static void addToHashtable(Hashtable<String,String> table , String key , String value ){
		if( key == null || key.trim().equals("")){
			return ;
		}
		
		if(value == null || value.trim().equals("")){
			return ;
		}
		
		if( table == null ){
			table = new Hashtable<String,String>() ;
		}
		
		table.put(key, value) ;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {

	}

}
